﻿using System;
using System.Collections.Generic;

namespace CommonHelper.Common.BinarySearchTree
{
    #region BSTUtils
    public static class BSTUtils
    {
        public static BinarySearchTree<T> GetABuiltBST<T>(IEnumerable<T> valuesToAdd) where T : IComparable<T>
        {
            BinarySearchTree<T> tree = new BinarySearchTree<T>();

            foreach (T i in valuesToAdd)
            {
                tree.Add(i);
            }

            return tree;
        }

        public static void TestStringBST()
        {
            BinarySearchTree<string> _tree = new BinarySearchTree<string>();

            int[] arr = { 1, 2, 3, 1, 4, 5, 2, 3, 6, 4, 8, 9, 10, 11, 34, 12, 11, 0, 6, 9, 6, 1, 1, 1, 4, 3, 6, 2, 1, 24, 5, 3, 6 };

            //int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };

            List<string> strArr = new List<string>(arr.Length);

            foreach (int i in arr)
            {
                strArr.Add(i.ToString());
            }
            foreach (int i in arr)
            {
                _tree.Add(i.ToString());
            }

            Console.WriteLine("Count: " + _tree.Count);

            Array.Sort(arr);

            foreach (int i in arr)
            {
                _tree.PrintValueStatusInBST(i.ToString());
            }

            _tree.Remove("11");
            Console.WriteLine("Count: " + _tree.Count);

            _tree.PrintValueStatusInBST("11");

            _tree.Remove("11");

            Console.WriteLine("Count: " + _tree.Count);

            _tree.PrintValueStatusInBST("11");

            _tree.PrintValueStatusInBST("50");

            _tree.Add("50");
            Console.WriteLine("Count: " + _tree.Count);

            _tree.PrintValueStatusInBST("50");

            _tree.Remove("51");

            Console.WriteLine("Count: " + _tree.Count);

            int count = 0;
            foreach (int i in arr)
            {
                Console.WriteLine(string.Format("Deleting {0} at {1}", i, count++));
                _tree.Remove(i.ToString());
            }

            Console.WriteLine("Count: " + _tree.Count);

            _tree.Remove("50");

            Console.WriteLine("Count: " + _tree.Count);

            _tree.Add("50");

            Console.WriteLine("Count: " + _tree.Count);
        }
    }
    #endregion
}
